package com.study.easyexcel.business.template;

import com.alibaba.excel.write.handler.context.CellWriteHandlerContext;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.DataFormat;
import org.apache.poi.ss.usermodel.Workbook;

import java.text.MessageFormat;

/**
 * 普通组数据的样式处理器，列表填充的样式处理器
 *
 * @author yuhuan
 * @date 2024/1/5
 */
public class SpecialGroupTemplate extends UniversalGroupStyle {

    /**
     * 用于定位单组数据的首行位置。通常由业务计算所得，适用于统计类公式的单元格定位，如果不涉及统计计算可忽略
     */
    private int originRow;

    /**
     * 用于定位单组数据的尾行位置。通常由业务计算所得，适用于统计类公式的单元格定位，如果不涉及统计计算可忽略
     */
    private int lastRow;

    @Override
    protected void setCellFormula(CellWriteHandlerContext context, Cell cell) {
        Workbook workbook = context.getWriteWorkbookHolder().getWorkbook();
        int rowIndex = cell.getRowIndex();
        DataFormat format = workbook.createDataFormat();
        short percentage = format.getFormat("0.00%");
        short twoDecimal = format.getFormat("0.00");
        CellStyle cellStyle = cell.getCellStyle();
        if (cellStyle == null) {
            cellStyle = workbook.createCellStyle();
        }
        cellStyle.setDataFormat(percentage);
        int columnIndex = cell.getColumnIndex();
        String fx = "";
        if (columnIndex == 4) {
            fx = MessageFormat.format("IF(C{0}=\"\",\"\",IF(C{0}=0,0,IFERROR(C{0}/D{0},\"\")))", rowIndex + 1);
        } else if (columnIndex == 8) {
            fx = MessageFormat.format("IF(G{0}=\"\",\"\",IF(G{0}=0,0,IFERROR(G{0}/H{0},\"\")))", rowIndex + 1);
        } else {
            fx = "未知开发";
        }
        cell.setCellFormula(fx);
    }

    public void setOriginRow(int originRow) {
        this.originRow = originRow;
    }

    public void setLastRow(int lastRow) {
        this.lastRow = lastRow;
    }
}
